package com.test;

import com.badlogic.gdx.jnigen.*;

import java.nio.ByteBuffer;

/**
 * Created by 石头哥哥 on 9/9/16.
 * PACKAGE_NAME:  com.test
 * COMMENT :
 */
public class MyJniClass {


    private static native ByteBuffer newDisposableByteBuffer (int numBytes); /*
   char* ptr = (char*)malloc(numBytes);
   return env->NewDirectByteBuffer(ptr, numBytes);
*/

    public static void main(String[] args) throws Exception {

        String rootPath = MyJniClass.class.getClassLoader().getResource("").getFile().toString();

        // generate C/C++ code
        new NativeCodeGenerator().generate(rootPath.replace("/target/test-classes/","/src/test/java/com/test/"), rootPath,rootPath.replace("/target/test-classes/","/src/main/java/cpp"), new String[] { "**/MyJniClass.java" }, null);

        // generate build scripts, for win32 only
//        BuildTarget win32 = BuildTarget.newDefaultTarget(BuildTarget.TargetOs.Windows, false);
//        win32.compilerPrefix = "mingw32-";
//        win32.compilerPrefix = "";
//        win32.cppFlags += " -g";
//        BuildTarget win64 = BuildTarget.newDefaultTarget(BuildTarget.TargetOs.Windows, true);
//        BuildTarget linux32 = BuildTarget.newDefaultTarget(BuildTarget.TargetOs.Linux, false);
//        BuildTarget linux64 = BuildTarget.newDefaultTarget(BuildTarget.TargetOs.Linux, true);
        BuildTarget mac = BuildTarget.newDefaultTarget(BuildTarget.TargetOs.MacOsX, false);
//        BuildTarget ios = BuildTarget.newDefaultTarget(BuildTarget.TargetOs.IOS, true);
//        BuildTarget android = BuildTarget.newDefaultTarget(BuildTarget.TargetOs.Android, false);
//        android.preCompileTask="/usr/local/Work/android-ndk-r11c";

        BuildConfig buildConfig = new BuildConfig("test");
        new AntScriptGenerator().generate(buildConfig, mac);
        String base=rootPath.replace("/target/test-classes/","/");

        // build natives
//        BuildExecutor.executeAnt(base+"jni/build-android32.xml", "-v -Dhas-compiler=true  clean postcompile");
        BuildExecutor.executeAnt(base+"jni/build-macosx64.xml", "-v -Dhas-compiler=true  clean postcompile");
//        BuildExecutor.executeAnt(base+"jni/build-ios32.xml", "-v -Dhas-compiler=true  clean postcompile");

        BuildExecutor.executeAnt(base+"jni/build.xml", "-v pack-natives");

        // load the test-natives.jar and from it the shared library, then execute the test.
        new JniGenSharedLibraryLoader(base+"libs/test-natives.jar").load("test");
//        System.loadLibrary("test64");
        ByteBuffer byteBuffer=MyJniClass.newDisposableByteBuffer(10);
//        ByteBuffer buffer = ByteBuffer.allocateDirect(1);
//        buffer.put(0, (byte)8);
//        MyJniClass.test(true, (byte)1, (char)2, (short)3, 4, 5, 6, 7, buffer, new boolean[] { false }, new char[] { 9 }, new short[] { 10 }, new int[] { 11 }, new long[] { 12 }, new float[] { 13 }, new double[] { 14 }, null, "Hurray");
    }
}
